package org.web.clientlistmanager.server.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration("accessControlListServerDBConfig")
@MapperScan(basePackages = "org.web.clientlistmanager.server.dao", sqlSessionFactoryRef = "clientListManagerServerSqlSessionFactory")
public class ClientListManagerServerDBConfig {


    @Bean("clientListManagerServerDataSource")
    @ConfigurationProperties(prefix = "org.web.clientlistmanager.server.jdbc")
    public DataSource clientListManagerServerDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        return dataSource;
    }

    @Bean("clientListManagerServerSqlSessionFactory")
    public SqlSessionFactory clientListManagerServerSqlSessionFactory(@Qualifier("clientListManagerServerDataSource") DataSource clientListManagerServerDataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clientListManagerServerDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/Mapper*.xml"));
        return sessionFactory.getObject();
    }
}
